package cn.wen.base.view.limit;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;

import cn.wen.base.R;


/**
 * 访淘宝限时换购头部---->>背景View
 */

public class LimitBgView extends View {

    private int mScreenWidth;
    private int mCenterWidth;
    private int mCenterColor = Color.RED;
    private int mItemCount = 5;// 控件分成几分
    private Paint mPaint;

    public LimitBgView(Context context) {
        this(context, null);
    }

    public LimitBgView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LimitBgView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.LimitBgView);
        mCenterColor = array.getColor(R.styleable.LimitBgView_limitBgCenterColor, mCenterColor);
        mItemCount = array.getInt(R.styleable.LimitBgView_limitBgItemCount, mItemCount);
        array.recycle();


        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        mScreenWidth = outMetrics.widthPixels;
        mCenterWidth = mScreenWidth / mItemCount;
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(mCenterColor);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 绘制中心部分的颜色
        int left = mCenterWidth * (mItemCount / 2);
        Rect rect = new Rect(left, 0, left + mCenterWidth, getMeasuredHeight());
        canvas.drawRect(rect, mPaint);
    }

}
